<?php

/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Controller', 'Controller');
App::uses('CakeEmail', 'Network/Email');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {

    public function beforeRender() {
        parent::beforeRender();
        $this->layout = 'brocker';
        
        $this->loadModel('Settings');
        $site_logo = $this->Settings->findByKey('site_logo');
        $site_email = $this->Settings->findByKey('site_email');
        $site_phone = $this->Settings->findByKey('site_phone');
        $facebook_link = $this->Settings->findByKey('facebook_link');
        $twitter_link = $this->Settings->findByKey('twitter_link');
        $linkedin_link = $this->Settings->findByKey('linkedin_link');
        $google_plus_link = $this->Settings->findByKey('google_plus_link');
        $rss_link = $this->Settings->findByKey('rss_link');
        $this->set('site_logo', $site_logo);
        $this->set('site_email', $site_email);
        $this->set('site_phone', $site_phone);
        $this->set('facebook_link', $facebook_link);
        $this->set('twitter_link', $twitter_link);
        $this->set('linkedin_link', $linkedin_link);
        $this->set('google_plus_link', $google_plus_link);
        $this->set('rss_link', $rss_link);
    }

    public $components = array(
        //'DebugKit.Toolbar',
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'pages',
                'action' => 'display'
            ),
            'logoutRedirect' => array(
                'controller' => 'pages',
                'action' => 'display',
                'home'
            ),
            'authenticate' => array(
                'Form' => array(
                    'fields' => array('username' => 'email', 'password' => 'password'),
                    'passwordHasher' => 'Blowfish'
                )
            )
        )
    );

    public function beforeFilter() {
        $this->Auth->allow('index', 'view', 'display', 'login_form', 'logout', 'import', 'values', 'fpass', 'rpass', 'login');
        if ($this->params['plugin'] == 'admin') {
            $this->Auth->allow('add', 'edit', 'delete');
        }
    }

    public function sendMail($mail_data) {
        $Email = new CakeEmail();
        $sender = (isset($mail_data['sender'])) ? $mail_data['sender'] : 'No-reply';
        $from = (isset($mail_data['from'])) ? $mail_data['from'] : 'no-reply@localhost.com';
        $Email->config('gmail');
        $Email->viewVars(array('data' => $mail_data['data']));
        $Email->template($mail_data['template'], 'default')
                ->subject($mail_data['subject'])
                ->emailFormat('html')
                ->from($from, $sender)
                ->to($mail_data['to'])
                ->send();
    }

}
